<template>
	<view class="auth">
		<view class="info">
			<view class="line">
				<view class="l">技能分类：</view>
				<uni-data-picker class="r" :localdata="items" popup-title="请选择分类" @change="onchange"></uni-data-picker>
			</view>
			<view class="line">
				<view class="l">技能标题：</view>
				<input type="text" class="r" v-model="name" />
			</view>
			<view class="line imgBox">
				<view class="l">技能介绍：</view>
				<textarea v-model="content" class="r" placeholder="请输入技能介绍" />
			</view>
			<view class="line imgBox" style="height: 140rpx;">
				<view class="l">技能单位：</view>
				<view class="unit">
					<u-radio-group 
					    v-model="unit"
					    placement="row">
						<u-radio activeColor="#FFCE54"  label="次" name="次" :customStyle="{marginRight: '20px'}"></u-radio>
						<u-radio activeColor="#FFCE54" label="天" name="天" :customStyle="{marginRight: '20px'}"></u-radio>
						<u-radio activeColor="#FFCE54" label="小时" name="小时" :customStyle="{marginRight: '20px'}"></u-radio>
						<u-radio activeColor="#FFCE54" label="分钟"name="分钟" ></u-radio>
					</u-radio-group>
					<input type="text" v-model="unit_price" placeholder="输入其他单位" style="margin-top: 50rpx;" />
				</view>
			</view>
			<view class="line">
				<view class="l">价格：</view>
				<input type="text" class="r" v-model="price" />
			</view>
			<view class="line imgBox">
				<view class="l">技能封面：</view>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="10"></u-upload>
				<!-- <view class="box">
					<image src="https://www.stck.cc/my/photo.png" mode="" class="icon"></image>
					<view class="word">上传图片</view>
				</view> -->
			</view>
			<view class="line imgBox">
				<view class="l">技能案例：</view>
				<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple
					:maxCount="10"></u-upload>
			</view>
			
		
		
			
		</view>
		<view class="btn" @click="save">提交审核</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				unit: '次',
				cate_id: '',
				name: '',
				content: '',
				unit_price: '',
				price: '',
				items: [],
				fileList1: [],
				fileList2: []
			};
		},
		onLoad() {
			this.getType()
		},
		methods:{
			onchange(e) {
				this.cate_id = e.detail.value[1].value
			},
			getType() {
				uni.$u.http.post('/getcitycate ', {}).then(res => {
					if (res.code == 1) {
						this.items = res.data
			
					}
				}).catch((res) => {
			
				})
			},
			save() {
				if(this.cate_id === ''){
					uni.$u.toast('请选择分类');
					return
				}
				if(this.name === ''){
					uni.$u.toast('请输入技能名称');
					return
				}
				if(!uni.$u.test.amount(this.price)){
					uni.$u.toast('请输入正确的金额');
					return
				}
				if(this.content === '') {
					uni.$u.toast('请输入技能介绍');
					return
				}
				
				let images = this.fileList1.map((value) => {
					return value.url.url
				})
				let images2 = this.fileList2.map((value) => {
					return value.url.url
				})
				if(images.length === 0) {
					uni.$u.toast('请上传技能封面');
					return
				}
				if(images2.length === 0) {
					uni.$u.toast('请上传案例');
					return
				}
				uni.$u.http.post('/postskill ', {
					cate_id: this.cate_id,
					name: this.name,
					price: this.price,
					content: this.content,
					unit_price: this.unit_price || this.unit,
					images: images.join(','),
					demoimages: images2.join(',')
				}).then(res => {
					if (res.code == 1) {
						uni.$u.toast(res.msg)
						let that = this
						uni.requestSubscribeMessage({
						  tmplIds: ['Pk6dSBHR0Pobiz6Ng4wZd0AgNep5NxvhFVpuNiZHjEo'],
						  success (res) {
							  console.log(res)
							  
						  },
						  complete() {
							  this.back()
						  }
						})
					}
				}).catch((res) => {
					if(res.code == 3){
						uni.$u.toast(res.msg)
						setTimeout(() => {
							// uni.$u.route({
							// 	url: 'pages/my/auth'
							// })
							uni.$u.route({
								url: 'pages/my/renZheng/geren'
							})
						},1000)
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log(result)
					if (result) {
						let item = this[`fileList${event.name}`][fileListLen]
						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: result
						}))
						fileListLen++
					} else {
						this[`fileList${event.name}`].splice(fileListLen, 1)
					}
			
				}
				console.log(this[`fileList${event.name}`])
			},
			uploadFilePromise(url) {
				let that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: that.Upload.uploadurl, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							type: 'image',
							token: uni.getStorageSync('token')
						},
						success: (res) => {
							let data = JSON.parse(res.data)
							if (res.statusCode === 200 && data.code === 1 && data.data) {
								resolve(data.data.url)
							} else {
								resolve()
							}
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background: #F6F6F6;
	}
.auth{
	background: linear-gradient(to bottom, #FFC141, #F6F6F6);
	background-size: 750rpx 500rpx;
	background-repeat: no-repeat;
	padding-top: 50rpx;
	.btn{
		background: linear-gradient(to right, #FFDF41, #FFC141);
		height: 88rpx;
		border: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 50rpx auto 0;
		width: 590rpx;
		border-radius: 44rpx;
	}
	.info{	
		background: #FFFFFF;
		margin: 0 20rpx;
		border-radius: 20rpx;
		padding: 0 20rpx 20rpx;
		
		.line{
			height: 100rpx;
			display: flex;
			align-items: center;
			border-bottom: 1px solid #F6F6F6;
			
			.l{
				color: #575859;
				font-size: 32rpx;
				width: 180rpx;
				text-align: right;
			}
			.r{
				flex: 1;
			}
		}
		.imgBox{
			height: auto;
			align-items: flex-start;
			padding: 20rpx 0;
			justify-content: flex-start;
		}
		.box{
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			height: 170rpx;
			width: 170rpx;
			border: 1px solid #FFCE54;
			border-radius: 20rpx;
			.icon{
				width: 51rpx;
				height: 49rpx;
			}
			.word{
				color: #FF8007;
				font-size: 30rpx;
				margin-top: 20rpx;
			}
		}
	}
}
</style>
